package com.zb.goods.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.goods.service.ConGoodsService;
import com.zb.result.Result;
import com.zb.service.ViewGoodsService;
import com.zb.service.ZjSpareIntroduceService;
import com.zb.service.ZjSpareService;
import com.zb.service.ZjSpareSpecInfoService;
import com.zb.utils.TokenUtil;
import com.zb.vo.GoodsBoomSelectVo;
import com.zb.vo.GoodsDetailSpecSelectVo;
import com.zb.vo.GoodsSelectVo;
import com.zb.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 备件表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-08-15
 */
@Api(tags = "商品备件类接口")
@RestController
@RequestMapping("/goods/spare")
public class ZjSpareController {
    @Resource
    private ConGoodsService conGoodsService;
    @Resource
    private ZjSpareService spareService;
    @Resource
    private ZjSpareSpecInfoService specInfoService;
    @Resource
    private ZjSpareIntroduceService introduceService;
    @Resource
    private ViewGoodsService viewGoodsService;

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品资料-商品管理-备件 - 新增或修改备件")
    @ApiOperation("新增或修改备件")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid GoodsVo goodsVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conGoodsService.save(loginInfo, 2, goodsVo);
        return Result.success(null == goodsVo.getId()? "新增备件成功" : "修改备件成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品资料-商品管理-备件 - 获取备件列表")
    @ApiOperation("获取备件列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody GoodsSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取备件列表成功").data(spareService.getList(custAccId, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品资料-商品管理-备件 - 根据备件ID获取备件详情")
    @ApiOperation("根据备件ID获取备件详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "备件ID", required = true) Long id) {
        return Result.success("获取备件详情成功！").data("row", spareService.getDetail(id));
    }

    @ApiOperation("根据备件ID获取产品修改前的信息")
    @GetMapping("/getInfo/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "备件ID", required = true) Long id) {
        return Result.success("获取产品信息成功").data("row", spareService.getInfo(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品资料-商品管理-备件 - 批量启用或禁用备件")
    @ApiOperation("批量启用或禁用备件")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "备件状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "商品ID集合", required = true) List<Long> idList) {
        spareService.updateBatchDisabledByIds(idList, status);
        return Result.success(status? "禁用备件成功" : "启用备件成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品资料-商品管理-备件 - 批量删除备件")
    @ApiOperation("批量删除备件")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "商品ID集合", required = true) List<Long> idList) {
        conGoodsService.deleteByIds(2, idList);
        return Result.success("删除备件成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品资料-商品管理-备件 - 根据备件ID获取产品参数")
    @ApiOperation("根据备件ID获取产品参数")
    @GetMapping("/getSpareParam/{id}")
    public Result getSpareParam(@PathVariable @ApiParam(value = "备件ID", required = true) Long id) {
        return Result.success("获取备件参数成功").data("row", spareService.getSpareParam(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品资料-商品管理-备件 - 根据备件ID获取规格列表")
    @ApiOperation("根据备件ID获取规格列表")
    @PostMapping("/getSpecList/{spareId}")
    public Result getSpecList(@PathVariable @ApiParam(value = "备件ID", required = true) Long spareId, @RequestBody GoodsDetailSpecSelectVo selectVo) {
        return Result.success("获取规格列表成功").data(specInfoService.getSpecList(spareId, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品资料-商品管理-备件 - 根据备件ID获取备件简介")
    @ApiOperation("根据备件ID获取备件简介")
    @GetMapping("/getIntroduce/{spareId}")
    public Result getIntroduce(@PathVariable @ApiParam(value = "备件ID", required = true) Long spareId) {
        return Result.success("获取备件简介成功").data("row", introduceService.getByProductId(spareId));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品资料-商品管理-备件 - 获取Boom关联的商品列表")
    @ApiOperation("获取Boom关联的商品列表")
    @PostMapping("/getGoodsBoomList")
    public Result getGoodsBoomList(HttpServletRequest request, @RequestBody @Valid GoodsBoomSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取Boom关联的商品成功").data(viewGoodsService.getGoodsBoomList(custAccId, 2, null, selectVo));
    }

}
